package com.learning.jquizenglish.screen.flashcard;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.FlashCardDetailModel;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.widget.smoothprogressbar.SmoothProgressBar;

public class FlashCardScreen extends JQuizEnglishFragment{

	private ViewPager mViewPager;
	private SmoothProgressBar mSmoothProgressBar;
	private View parent;
	private long cateId;
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		parent = LayoutInflater.from(getActivity()).inflate(R.layout.screen_flashcard, container,false);
		mViewPager = (ViewPager) parent.findViewById(R.id.pager);
		mSmoothProgressBar = (SmoothProgressBar) parent.findViewById(R.id.progressbar);
		cateId = getArguments().getLong("id");
		requestData();
		return parent;
	}
	
	public void requestData(){
		JQuizRequestModel requestModel = JQuizEnglishApi.getRequestFlashCardDetail(cateId);
		executeGetTask(requestModel, false, FlashCardDetailModel.class, new JQuizTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				parent.setBackgroundColor(getResources().getColor(R.color.mau_trang));
				if(object!=null){
					FlashCardDetailModel detailModel = (FlashCardDetailModel) object;
					FlashCardStore.getInstance().setListCard(detailModel.terms);
					mViewPager.setAdapter(new CardSliderAdapter(getFragmentManager()));
				}
				mSmoothProgressBar.setVisibility(View.GONE);
				mViewPager.setVisibility(View.VISIBLE);
			}
		});
	}
	
	class CardSliderAdapter extends FragmentStatePagerAdapter{
		int cardSize;
		public CardSliderAdapter(FragmentManager fm) {
			super(fm);
			cardSize = FlashCardStore.getInstance().getListCard().size();
		}

		@Override
		public Fragment getItem(int position) {
			return CardFragment.createCard(position);
		}

		@Override
		public int getCount() {
			return cardSize;
		}
		
		@Override
		public CharSequence getPageTitle(int position) {
			return position + 1 + "/" + cardSize;
		}
		
	}

}
